package cn.hs.lyxf.web.controller;

import cn.hs.lyxf.domain.Maintenance;
import cn.hs.lyxf.query.MaintenanceQuery;
import cn.hs.lyxf.result.JsonResult;
import cn.hs.lyxf.result.PageList;
import cn.hs.lyxf.service.IMaintenanceService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/maintenance")
public class MaintenanceController {

    @Autowired
    public IMaintenanceService maintenanceService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody Maintenance maintenance) {
        if (maintenance.getId() != null) {
            maintenanceService.updateById(maintenance);
        } else {
            maintenanceService.insert(maintenance);
        }
        return JsonResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id) {
        maintenanceService.deleteById(id);
        return JsonResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id") Long id) {
        return JsonResult.success(maintenanceService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult list() {
        return JsonResult.success(maintenanceService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JsonResult page(@RequestBody MaintenanceQuery query) {
        Page<Maintenance> page = new Page<Maintenance>(query.getPage(), query.getRows());
        page = maintenanceService.selectPage(page);
        return JsonResult.success(new PageList<Maintenance>(page.getTotal(), page.getRecords()));
    }

    /**
     * 用户签约维保
     */
    @RequestMapping(value = "/user/signed/{userId}", method = RequestMethod.GET)
    public JsonResult userSigned(@PathVariable("userId") Long userId) {
        return maintenanceService.userSigned(userId);
    }

    /**
     * 分配给维保员维保任务
     */
    @RequestMapping(value = "/distribution", method = RequestMethod.GET)
    public JsonResult distribution(@RequestBody Maintenance maintenance) {
        return maintenanceService.distribution(maintenance);
    }

}
